<template>
  <div>
    <p style="display: flex">
      <el-button type="info" icon="el-icon-refresh" size="mini"
                 @click="list()">刷新
      </el-button>
      <el-button type="info" size="mini"
                 @click="dialogVisible=true">添加
      </el-button>
      <el-button type="info" icon="el-icon-refresh" size="mini"
                 @click="list()">刷新
      </el-button>
    </p>
    <p style="display: flex">
      <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pageInfo.size"
          :current-page="pageInfo.page"
          @current-change="list"
          :total="pageInfo.total">
      </el-pagination>
      <el-select v-model="pageInfo.size" placeholder="请选择"
                 @change="list(1)">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"

            :value="item.value">
        </el-option>
      </el-select>
    </p>
    <div>
      <el-dialog title="添加讲师" :visible.sync="dialogVisible">
        <el-form :model="form">

          <el-form-item label="姓名" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>


          <el-form-item label="昵称" :label-width="formLabelWidth">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="头像" :label-width="formLabelWidth">
            <el-upload
                class="upload-teacher-image"
                action="http://localhost:9000/file/admin/upload"
                :on-success="uploadImageSuccess"
                :data="uploadData"
                show-file-list="false">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>


          <el-form-item label="职位" :label-width="formLabelWidth">
            <el-input v-model="form.position" autocomplete="off"></el-input>
          </el-form-item>


          <el-form-item label="座右铭" :label-width="formLabelWidth">
            <el-input v-model="form.motto" autocomplete="off"></el-input>
          </el-form-item>


          <el-form-item label="简介" :label-width="formLabelWidth">
            <el-input v-model="form.intro" autocomplete="off"></el-input>
          </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible= false">取 消</el-button>
          <el-button type="primary" @click="saveTeacher">确 定</el-button>
        </div>
      </el-dialog>
      <div class="teacher-container">
        <teacher-card v-for="(teacher,index) in pageInfo.list" :key="teacher" :teacher="teacher"
                      @deleteTeacher="del"
                      class="teacher-card"
                      @editTeacher="edit">
          {{ index }}
        </teacher-card>
      </div>
    </div>

  </div>
</template>

<script>
import {list, deleteTeacher, saveTeacher} from "@/network/teacher";
import {Confirm} from '@/assets/js/confirm'
import {Validator} from "@/assets/js/validator";
import TeacherCard from "@/views/admin/teacher/child/TeacherCard";

export default {
  name: "teacher",
  components: {TeacherCard},
  mounted() {
    this.list(1)
  },
  data() {
    return {
      pageInfo: {
        page: 1,
        size: 1,
        list: [],
        total: 0,
      }, options: [
        {
          value: 1,
          label: 1
        },
        {
          value: 5,
          label: 5
        },
        {
          value: 10,
          label: 10
        },
        {
          value: 50,
          label: 50
        },],
      dialogVisible: false,
      form: {},
      formLabelWidth: '120px',
      fileList: [],
      uploadData: {
        use: FILE_USE.TEACHER.key
      }
    }
  },
  methods: {
    list(page) {
      if (page === null || page === undefined) {
        page = this.pageInfo.page
      }
      let _this = this;
      list(page, _this.pageInfo.size).then(res => {
        _this.pageInfo = res.data
      })
    },
    saveTeacher() {
      let _this = this;
      // 保存校验
      if (1 !== 1
          || !Validator.require(_this.form.name, "姓名")
          || !Validator.length(_this.form.name, "姓名", 1, 50)
          || !Validator.length(_this.form.nickname, "昵称", 1, 50)
          || !Validator.length(_this.form.image, "头像", 1, 100)
          || !Validator.length(_this.form.position, "职位", 1, 50)
          || !Validator.length(_this.form.motto, "座右铭", 1, 50)
          || !Validator.length(_this.form.intro, "简介", 1, 500)
      ) {
        return;
      }

      saveTeacher(_this.form).then(res => {
        _this.$message.success("保存成功");
        _this.form = {}
        _this.dialogVisible = false;
        _this.list()
      })
    },
    edit(row) {
      console.log(row);
      let _this = this;
      _this.form = Object.assign({}, row)
      _this.dialogVisible = true;
    },
    del(row) {
      let _this = this;
      Confirm.show('此操作将永久删除该文件, 是否继续?', function () {
        deleteTeacher(row.id).then(res => {
          _this.list();
          _this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      })

    },
    uploadImageSuccess(res, file) {
      let _this = this;
      _this.form.image = res.data;
    }
  }
}
</script>

<style scoped>
.teacher-container {
  display: flex;
}

.teacher-card {
  width: 25%;
  margin: 4px;

}
</style>
